'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            if($state.includes('**.frequency.update')){
                title="编辑广告频率";
                var id = $state.params.id;
                detail(id);

            }else if($state.includes('**.frequency.create')){
                title="添加广告频率";
                $scope.searchParams = { };
                $scope.loading = true;
                $scope.isDisabled = false;
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.searchParams.adType=$("#adType").val();
                $scope.searchParams.maxNum=$("#maxNum").val();
                $scope.searchParams.minNum=$("#minNum").val();
                $scope.searchParams.isEnable=$('#isEnable').val()
                var maxNum=$('#maxNum').val();
                var minNum=$('#minNum').val();
                if(isNaN(minNum)|| parseInt(minNum)<=0||!(/^\d+$/.test(minNum))){
                    $scope.alertModal({'content':'数据格式不正确,只能输入整数'});
                    return ;
                }
                if(isNaN(maxNum)|| parseInt(maxNum)<=0||!(/^\d+$/.test(maxNum))){
                    $scope.alertModal({'content':'数据格式不正确,只能输入整数'});
                    return ;
                }
                if(parseInt(minNum)< 0){
                    $scope.alertModal({'content':'最小值必须大于0'});
                    return;
                }
                if(!$('#maxNum').val()){
                    $scope.alertModal({'content':'最大值不能为空'});
                    return;
                }

                if(!$('#minNum').val()){
                    $scope.alertModal({'content':'最小值不能为空'});
                    return;
                }


                if(isNaN($('#maxNum').val())){
                    $scope.alertModal({'content':'最大值格式不正确'});
                    return;
                }
                if(isNaN($('#minNum').val())){
                    $scope.alertModal({'content':'最小值格式不正确'});
                    return;
                }

                if(!$('#isEnable').val()){
                    $scope.alertModal({'content':'状态不能为空'});
                    return;
                }

                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.id ? $scope.app.project_name+'/frequency/read/update' : $scope.app.project_name+'/frequency/read/add',
                        data: $scope.searchParams
                    }).then(callback);
                }
                function callback(result){
                    console.log(result);
                    if(result.httpCode==200){
                        $scope.alertModal({"content":"操作成功"});
                            $timeout(function(){
                                $state.go('main.hui.frequency.list');
                            },2000);
                    }else{

                        if(result.record.resCode==10006){
                            $scope.alertModal({"content":result.record.resMsg});

                        }else{
                            $scope.alertModal({"content":"操作失败"});
                                $scope.isDisabled = false;
                        }

                    }


                    $scope.loading = false;
                }
            }

            // 初始化页面
            function detail(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/frequency/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }



        }]);